//
//  JNHomeViewController.m
//  JNNetEestNews
//
//  Created by Jyougen on 2017/3/10.
//  Copyright © 2017年 Jyougen. All rights reserved.
//

#import "JNHomeViewController.h"
#import "JNChannelModel.h"
#import "ChannelLabel.h"
#import "JNNewsCollectionViewCell.h"

@interface JNHomeViewController () <UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIScrollView *channelScrollView;
@property (weak, nonatomic) IBOutlet UICollectionView *newsCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) NSArray <JNChannelModel *>* channelDataArr;

@end

@implementation JNHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupChannelUI];
    NSLog(@"%@",self.channelDataArr);
    [self setupNewsUI];
}

//频道视图
- (void)setupChannelUI{

    self.channelDataArr = [JNChannelModel getModelArr];
    
    CGFloat labelHeight = 44;
    CGFloat labelWidth = 80;
    
    for (NSInteger i = 0; i < self.channelDataArr.count; i++) {
        
        ChannelLabel * label = [[ChannelLabel alloc] initWithFrame:CGRectMake(i * labelWidth , 0, labelWidth, labelHeight)];
        [self.channelScrollView addSubview: label];
        label.text = self.channelDataArr[i].tname;
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
    }

    self.channelScrollView.contentSize = CGSizeMake(labelHeight * self.channelDataArr.count, 0);
    
}

- (void)setupNewsUI{

    self.newsCollectionView.dataSource = self;
    
    self.flowLayout.itemSize = self.newsCollectionView.bounds.size;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    
    self.newsCollectionView.bounces = NO;
    self.newsCollectionView.showsHorizontalScrollIndicator = NO;
    self.newsCollectionView.pagingEnabled = YES;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{


    return self.channelDataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    JNNewsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Jyougen" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
    
    cell.urlStr = [NSString stringWithFormat:@"%@/0-20.html",self.channelDataArr[indexPath.item].tid];
    
    NSLog(@"%@",[NSString stringWithFormat:@"%@/0-20.html",self.channelDataArr[indexPath.item].tid]);
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
